package cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.dispatchvehicle.DispatchVehicleDO;
import cn.iocoder.yudao.module.erp.service.dispatchvehicle.DispatchVehicleService;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;
import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;



@Tag(name = "管理后台 - 总配车信息")
@RestController
@RequestMapping("/erp/dispatch-vehicle")
@Validated
public class DispatchVehicleController {

    @Resource
    private DispatchVehicleService dispatchVehicleService;

    @PutMapping("/update")
    @Operation(summary = "更新总配车信息")
    // @PreAuthorize("@ss.hasPermission('erp:dispatch-vehicle:update')")
    public CommonResult<Boolean> updateDispatchVehicle(@Valid @RequestBody DispatchVehicleSaveReqVO updateReqVO) {
        dispatchVehicleService.updateDispatchVehicle(updateReqVO);
        return success(true);
    }

    @GetMapping("/dispatchVehicleList")
    @Operation(summary = "获得ERP运输单列表")
    // @PreAuthorize("@ss.hasPermission('erp:dispatch-vehicle:query')")
    public CommonResult<List<DispatchVehicleDO>> dispatchVehicleList(@RequestParam("line") Integer line,@RequestParam("dpId") String dpId) {
        List<DispatchVehicleDO> dispatchVehicleList = dispatchVehicleService.dispatchVehicleList(line,dpId);
        return success(dispatchVehicleList);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除总配车信息")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('erp:dispatch-vehicle:delete')")
    public CommonResult<Boolean> deleteDispatchVehicle(@RequestParam("id") Long id) {
        dispatchVehicleService.deleteDispatchVehicle(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得总配车信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('erp:dispatch-vehicle:query')")
    public CommonResult<DispatchVehicleRespVO> getDispatchVehicle(@RequestParam("id") Long id) {
        DispatchVehicleDO dispatchVehicle = dispatchVehicleService.getDispatchVehicle(id);
        return success(BeanUtils.toBean(dispatchVehicle, DispatchVehicleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得总配车信息分页")
    // @PreAuthorize("@ss.hasPermission('erp:dispatch-vehicle:query')")
    public CommonResult<PageResult<DispatchVehicleRespVO>> getDispatchVehiclePage(@Valid DispatchVehiclePageReqVO pageReqVO) {
        PageResult<DispatchVehicleDO> pageResult = dispatchVehicleService.getDispatchVehiclePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DispatchVehicleRespVO.class));
    }

    @GetMapping("/outer/page")
    @Operation(summary = "分页获得所有配车出库列表")
    // @PreAuthorize("@ss.hasPermission('erp:dispatch-vehicle-outer:query')")
    public CommonResult<PageResult<DispatchVehicleRespVO>> getDispatchVehicleOuterPage(@Valid DispatchVehiclePageReqVO pageReqVO) {
        PageResult<DispatchVehicleDO> pageResult = dispatchVehicleService.getDispatchVehicleOuterPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DispatchVehicleRespVO.class));
    }

    @GetMapping("/transport/page")
    @PermitAll
    @Operation(summary = "分页获得所有配车的运踪列表")
//    @PreAuthorize("@ss.hasPermission('erp:dispatch-vehicle:query')")
    public CommonResult<PageResult<DispatchVehicleRespVO>> getTransportPage(@Valid DispatchVehiclePageReqVO pageReqVO) {
        PageResult<DispatchVehicleDO> pageResult = dispatchVehicleService.getTransportPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DispatchVehicleRespVO.class));
    }

    @PostMapping("lineDetails/update")
    @Operation(summary = "批量修改各配车运踪线路详情")
//    @PreAuthorize("@ss.hasPermission('erp:dispatch-vehicle:update')")
    public CommonResult<Boolean> lineDetailsUpdate(@Valid @RequestBody List<DispatchVehicleSaveReqVO> createReqVO) {
        dispatchVehicleService.lineDetailsUpdate(createReqVO);
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出总配车信息 Excel")
    // @PreAuthorize("@ss.hasPermission('erp:dispatch-vehicle:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDispatchVehicleExcel(@Valid DispatchVehiclePageReqVO pageReqVO,
                                           HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DispatchVehicleDO> list = dispatchVehicleService.getDispatchVehiclePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "总配车信息.xls", "数据", DispatchVehicleRespVO.class,
                BeanUtils.toBean(list, DispatchVehicleRespVO.class));
    }

}
